import bb.cascades 1.2
import bb.data 1.0

import "lib"
import "controls"
Page {
    property variant item
    actionBarVisibility: ChromeVisibility.Overlay
    
    function parseHtml(html) {
        html = html.replace(/width\=\"\d{0,}\"/g, "")
        html = html.replace(/height\=\"\d{0,}\"/g, "")
        html = html.replace(/<p>/g, "<p class='content'>")
        return html;
    }
    
    onItemChanged: {
        m_title.text = item.title
        m_source.text = qsTr("%1  %2").arg(item.updateTime).arg(item.source)
        dataSource.source = item.link.url;
    }
    
    ScrollView {
        Padding {
            Container {
                verticalAlignment: VerticalAlignment.Fill
                horizontalAlignment: HorizontalAlignment.Fill
                Container {
                    Label {
                        id: m_title
                        multiline: true
                        textStyle.base: com.titleStyle
                    }
                }
                
                Container {
                    Label {
                        id: m_source
                        textStyle.base: com.descStyle
                    }
                }
                
                Divider {
                }
                
                ProgressIndicator {
                    id: prog
                    fromValue: 0
                    toValue: 100
                    visible: value<100
                }
                
                WebView {
                    id: m_body
                    verticalAlignment: VerticalAlignment.Fill
                    horizontalAlignment: HorizontalAlignment.Fill
                    implicitLayoutAnimationsEnabled: false
                    settings.credentialAutoFillEnabled: true
                    settings.formAutoFillEnabled: true
                    settings.activeTextEnabled: false
                    settings.viewport: {
                        "width": "device-width",
                        "initial-scale": 1.0
                    }
                    
                    onLoadProgressChanged: {
                        prog.value = loadProgress
                    }
                    settings.userStyleSheetLocation: "asset:///controls/DocView.css"
                }
            }
            
            Container {
                preferredHeight: 50
            }
        }
    }
    
    attachedObjects: [
        Common {
            id: com
        },
        
        DataSource {
            id: dataSource
            onSourceChanged: {
                dataSource.load()
            }
            onDataLoaded: {
                m_body.html = parseHtml(data.body.text);
            }
            
            onError: {
                _Setting.alert(qsTr("Load Error."))
            }
        }
    ]
}
